//数据准备
var jsonObj = {data:[
    {
         username:'1001',
         name:'张三',
         sex:'女',
         password:'1234',
         age:'29',
         birthday:'1991-1-1'
    },
    {
         username:'1002',
         name:'李四',
         sex:'女',
         password:'1234',
         age:'28',
         birthday:'1992-2-2'
    },
     {
         username:'1003',
         name:'王五',
         sex:'男',
         password:'1234',
         age:'27',
         birthday:'1993-3-3'
     },
     {
         username:'1004',
         name:'赵六',
         sex:'女',
         password:'1234',
         age:'26',
         birthday:'1994-4-4'
     }
 ]}

//初始化
function init(){
    var table = ""; 
    for(var i = 0;i<jsonObj.data.length;i++){
        var data = jsonObj.data[i];
        var username = data.username;
        var name = data.name;
        var sex = data.sex;
        var password = data.password;
        var age = data.age;
        var birthday = data.birthday;
        table = table + "<td><input type=\"checkbox\" name = \"check\"></td>";
        table = table + "<td>"+ username +"</td><td>" + name + "</td><td> " + sex + "</td><td>"+ password + "</td><td>" + age + "</td><td>" + birthday + "</td>";
        table = "<tr>" + table + "</tr>";
    }
    $("#table").html(table);
}

function empty(){
    $("#user").val("");
    $("#nam").val("");
    $("#sex").val("");
    $("#password").val("");
    $("#age").val("");
    $("#birthday").val("");
}
//新增
function add(){
     $(".modal-add").show(200,function () {
        empty();
        $('.modal-add .title_v3_l').text('新增元数据');  //修改弹层名称
        $('.modal-add').one('click','.sure-btn',function () {  //点击确定按钮获取数据
            $('.modal').hide(200);
            var text = {
                username:$("#user").val(),
                name:$("#nam").val(),
                sex:$("#sex").val(),
                password:$("#password").val(),
                age:$("#age").val(),
                birthday:$("#birthday").val()
            };
            var value = $.trim(text.name); // trim方法删除前后空格
            if(value===''){    //名称不为空
                alert('请输入名称');
                return;
            }
            jsonObj.data.push(text);
            init();
        });
    });

     //关闭弹层
     $(document).on('click','.modal .close',function () {
        $(this).parents('.modal').hide(200);
    });
}
//删除
function detle(){
    //判断单选框是否被选中
    $("input[name='check']:checked").each(function(){
        n = $(this).parents("tr").index()-3;
        jsonObj.data.splice(n,1);//移出
        init();
    })
}
//编辑
function edit(){
    //判断单选框是否被选中
    $("input[name='check']:checked").each(function(){
        n = $(this).parents("tr").index()-3;
        var data = jsonObj.data[n];
        //弹窗显示
        $(".modal-add").show(200,function () {
            $('.modal-add .title_v3_l').text('编辑元数据');  //修改弹层名称
            //赋值
            $("#user").val(data.username);
            $("#nam").val(data.name);
            $("#sex").val(data.sex);
            $("#password").val(data.password);
            $("#age").val(data.age);
            $("#birthday").val(data.birthday);
            $('.modal-add').one('click','.sure-btn',function () {  //点击确定按钮获取数据
                $('.modal').hide(200);
                var text = {
                    username:$("#user").val(),
                    name:$("#nam").val(),
                    sex:$("#sex").val(),
                    password:$("#password").val(),
                    age:$("#age").val(),
                    birthday:$("#birthday").val()
                };
                data.username = text.username;
                data.name = text.name;
                data.sex = text.sex;
                data.password = text.password;
                data.age = text.age;
                data.birthday = text.birthday;
                init();//初始化
            });
        })
    })
}

//查询
function query(){
    var username = document.getElementById("username").value;
    var name = document.getElementById("name").value;
    var text = "";
    if (username != ""){
        text = username;
    }else{
        text = name;
    }
    $('table tbody tr').hide().filter(':contains('+text+')').show();
}


